﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public interface IDataExpressionInputModel
    {
        string Value
        {
            get;
        }

        string Encoded
        {
            set;
        }

        string Encrypted
        {
            set;
        }
    }

    public interface IPersistentDataExpressionModel
    {
        string Value
        {
            get;
        }

        string Encoded
        {
            set;
        }

        string Encrypted
        {
            set;
        }
    }

    [DataContract]
    public class DataExpressionInputModel : IDataExpressionInputModel, IPersistentDataExpressionModel
    {
        private DataExpression _dataExpression;
        private string _postedEncryptedValue;

        public DataExpressionInputModel()
        {
            _dataExpression = new DataExpression();
            _postedEncryptedValue = _dataExpression.Encrypted;
        }

        public DataExpressionInputModel(string value)
        {
            _dataExpression = new DataExpression(value);
            _postedEncryptedValue = _dataExpression.Encrypted;
        }

        public DataExpressionInputModel(DataExpression expression)
        {
            if (expression == null)
            {
                expression = new DataExpression();
            }

            _dataExpression = expression;
            _postedEncryptedValue = _dataExpression.Encrypted;
        }

        [ReadOnly(true), DataMember]
        public string Value
        {
            get
            {
                if (string.IsNullOrEmpty(_postedEncryptedValue) && string.IsNullOrEmpty(_dataExpression.Encoded))
                {
                    return null;
                }
                else if (!_dataExpression.IsValid(_postedEncryptedValue))
                {
                    throw new Exception("Input has been tampered with.");
                }
                else
                {
                    return _dataExpression.Value;
                }
            }
        }

        [DataMember]
        public string Encoded
        {
            get
            {
                // Nothing should ever call this except for mvc model binders (must be public for model binding to work).
                return _dataExpression.Encoded;
            }
            set
            {
                _dataExpression.Encoded = value;
            }
        }

        [DataMember]
        public string Encrypted
        {
            get
            {
                // Nothing should ever call this except for mvc model binders (must be public for model binding to work).
                return _postedEncryptedValue;
            }
            set
            {
                _postedEncryptedValue = value;
            }
        }
    }
}
